<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"xuarmy.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="1. 视图创建&#x2F;初始化1.1. xib创建UIViewController通过xib文件创建UIViewController会调用这个方法。 Objec-C代码示例 123456- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil &amp;#123;  if (self &amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="UIViewController的生命周期">
<meta property="og:url" content="http://xuarmy.gitee.io/blog/2020/08/04/UIViewController%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/index.html">
<meta property="og:site_name" content="代号·破局">
<meta property="og:description" content="1. 视图创建&#x2F;初始化1.1. xib创建UIViewController通过xib文件创建UIViewController会调用这个方法。 Objec-C代码示例 123456- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil &amp;#123;  if (self &amp;#x">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-04T12:40:41.000Z">
<meta property="article:modified_time" content="2020-08-04T13:05:23.524Z">
<meta property="article:author" content="xuarmy">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://xuarmy.gitee.io/blog/2020/08/04/UIViewController%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>UIViewController的生命周期 | 代号·破局</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">代号·破局</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">学无先后·达者为先</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xuarmy.gitee.io/blog/2020/08/04/UIViewController%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/photo.jpg">
      <meta itemprop="name" content="xuarmy">
      <meta itemprop="description" content="木を植える一番いい時間は十年前、次は今です。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="代号·破局">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          UIViewController的生命周期
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-08-04 20:40:41 / 修改时间：21:05:23" itemprop="dateCreated datePublished" datetime="2020-08-04T20:40:41+08:00">2020-08-04</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="1-视图创建-初始化"><a href="#1-视图创建-初始化" class="headerlink" title="1. 视图创建/初始化"></a>1. 视图创建/初始化</h1><h2 id="1-1-xib创建UIViewController"><a href="#1-1-xib创建UIViewController" class="headerlink" title="1.1. xib创建UIViewController"></a>1.1. xib创建UIViewController</h2><p>通过xib文件创建<code>UIViewController</code>会调用这个方法。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil &#123;</span><br><span class="line">  if (self &#x3D; [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) &#123;</span><br><span class="line">    &#x2F;&#x2F; TODO:</span><br><span class="line">  &#125;</span><br><span class="line">  return self;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="keyword">init</span>(nibName nibNameOrNil: <span class="type">String?</span>, bundle nibBundleOrNil: <span class="type">Bundle?</span>) &#123;</span><br><span class="line">  <span class="keyword">super</span>.<span class="keyword">init</span>(nibName: nibNameOrNil, bundle: nibBundleOrNil)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-2-StoryBoard串联图"><a href="#1-2-StoryBoard串联图" class="headerlink" title="1.2. StoryBoard串联图"></a>1.2. StoryBoard串联图</h2><p>使用<code>StoryBoard</code>串联视图管理，程序不会直接初始化一个<code>UIViewController</code>，<code>StoryBoard</code>会自动初始化并调用<code>initWithCoder</code>。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (instancetype)initWithCoder:(NSCoder *)aDecoder &#123;</span><br><span class="line">    if (self &#x3D; [super initWithCoder:aDecoder]) &#123;</span><br><span class="line">    &#x2F;&#x2F; TODO:</span><br><span class="line">   &#125;</span><br><span class="line">  return self;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">required</span> <span class="keyword">init</span>?(coder: <span class="type">NSCoder</span>) &#123;</span><br><span class="line">  <span class="keyword">super</span>.<span class="keyword">init</span>(coder: coder)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="2-视图加载"><a href="#2-视图加载" class="headerlink" title="2. 视图加载"></a>2. 视图加载</h1><h2 id="2-1-xib-storyboard-文件加载完了"><a href="#2-1-xib-storyboard-文件加载完了" class="headerlink" title="2.1. xib(storyboard)文件加载完了"></a>2.1. xib(storyboard)文件加载完了</h2><p>这个方法是，配合上述两个视图控制器实例化方法使用的，如果有些特殊需求，需要根据实际情况进行内容的设置，不能在<code>xib</code>或<code>storyBoard</code>中加载的内容，就可以在<code>awakeFromNib</code>方法中进行加载。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)awakeFromNib &#123;</span><br><span class="line">  [super awakeFromNib];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="class"><span class="keyword">class</span> <span class="title">func</span> <span class="title">awakeFromNib</span>() </span>&#123;</span><br><span class="line">  <span class="keyword">super</span>.awakeFromNib()</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-2-加载视图"><a href="#2-2-加载视图" class="headerlink" title="2.2. 加载视图"></a>2.2. 加载视图</h2><p>当<code>UIViewController</code>的<code>view</code>被访问且<code>view = nil</code>的时候，调用<code>loadView</code>方法。所以<code>loadView</code>方法在视图控制器的生命周期内可能会被多次调用（大多数情况只调用一次）。Apple对该API解释的时候强调<code>loadView</code>方法永远不要直接调用，而是由系统自动调用。<br>调用方法会加载或创建一个<code>view</code>并把它赋值给<code>UIViewController</code>的<code>view</code>。<br>在创建<code>view</code>的时，首先会根据<code>nibName</code>加载<code>nib</code>文件。如果<code>nibName</code>为空或找不到对应的<code>nib</code>文件，则会创建一个空视图（纯代码编写的场合）。  </p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">- (void)loadView &#123;</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">loadView</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>  </p>
<ul>
<li>永远不要直接调用这个方法（系统自动调用）</li>
<li>重写这个方法的时候不要调用父类的该方法</li>
</ul>
<h2 id="2-3-视图加载完成"><a href="#2-3-视图加载完成" class="headerlink" title="2.3. 视图加载完成"></a>2.3. 视图加载完成</h2><p>当<code>loadView</code>将<code>view</code>载入内存中，会调用<code>viewDidLoad</code>方法，对视图来进行设置。比如：数据载入、初始设定、修改约束、隐藏视图、移除视图等。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">  [super viewDidLoad];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewDidLoad</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewDidLoad()</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-4-视图将要出现"><a href="#2-4-视图将要出现" class="headerlink" title="2.4. 视图将要出现"></a>2.4. 视图将要出现</h2><p>系统内容载入所有数据后，将要在屏幕上显示视图时，调用这个方法，对即将显示的视图做进一步的设置。比如：根据设备方向修改视图的约束、设置视图显示样式、更新数据等。<br>在视图切换时，视图的每次将要重新显示，该方法都会被调用（<code>push</code>压栈的下一级视图<code>pop</code>出栈后，重新回到该视图时）。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewWillAppear:(BOOL)animated &#123;</span><br><span class="line">  [super viewWillAppear:animated];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewWillAppear</span><span class="params">(<span class="number">_</span> animated: Bool)</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewWillAppear(animated)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-5-子视图将要布局"><a href="#2-5-子视图将要布局" class="headerlink" title="2.5. 子视图将要布局"></a>2.5. 子视图将要布局</h2><p><code>view</code> 将要布局<code>Subviews</code>。比如：<code>view</code>的<code>bounds</code>改变（状态栏显示/隐藏，视图方向等变化）后，调整<code>Subviews</code>的布局前，要做的工作在该方法中处理。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewWillLayoutSubviews &#123;</span><br><span class="line">  [super viewWillLayoutSubviews];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewWillLayoutSubviews</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewWillLayoutSubviews()</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-6-子视图布局完成"><a href="#2-6-子视图布局完成" class="headerlink" title="2.6. 子视图布局完成"></a>2.6. 子视图布局完成</h2><p><code>view</code> 完成<code>Subviews</code>布局。比如：<code>view</code>的<code>bounds</code>改变（状态栏显示/隐藏，视图方向等变化）后，调整<code>Subviews</code>的布局完成后，要做的工作在该方法中处理。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewDidLayoutSubviews &#123;</span><br><span class="line">  [super viewDidLayoutSubviews];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewDidLayoutSubviews</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewDidLayoutSubviews()</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-7-视图已经显示"><a href="#2-7-视图已经显示" class="headerlink" title="2.7. 视图已经显示"></a>2.7. 视图已经显示</h2><p>已经在屏幕上显示视图时，调用这个方法，对正在显示的视图做进一步的设置。比如：视图展示效果（动画）的处理。<br>在视图切换时，视图的每次重新显示完了，该方法都会被调用（<code>push</code>压栈的下一级视图<code>pop</code>出栈后，重新回到该视图时）。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewDidAppear:(BOOL)animated &#123;</span><br><span class="line">  [super viewDidAppear:animated];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewDidAppear</span><span class="params">(<span class="number">_</span> animated: Bool)</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewDidAppear(animated)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="3-视图卸载"><a href="#3-视图卸载" class="headerlink" title="3. 视图卸载"></a>3. 视图卸载</h1><h2 id="3-1-视图将要消失"><a href="#3-1-视图将要消失" class="headerlink" title="3.1. 视图将要消失"></a>3.1. 视图将要消失</h2><p>在视图切换时，视图即将被移除时调用该方法。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewWillDisappear:(BOOL)animated &#123;</span><br><span class="line">  [super viewWillDisappear:animated];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewWillDisappear</span><span class="params">(<span class="number">_</span> animated: Bool)</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewWillDisappear(animated)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-2-视图已经消失"><a href="#3-2-视图已经消失" class="headerlink" title="3.2.视图已经消失"></a>3.2.视图已经消失</h2><p>在视图切换时，视图已经被移除后调用该方法。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewDidDisappear:(BOOL)animated &#123;</span><br><span class="line">  [super viewDidDisappear:animated];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">viewDidDisappear</span><span class="params">(<span class="number">_</span> animated: Bool)</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.viewDidDisappear(animated)</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="4-视图销毁"><a href="#4-视图销毁" class="headerlink" title="4. 视图销毁"></a>4. 视图销毁</h1><p>视图被销毁后调用该方法，比如：释放<code>init</code>和<code>viewDidLoad</code>中创建的对象。ARC的场合通常是：移除通知观察者、移除委托引用、<code>NSTimer</code>销毁、单类的其他操作等。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">- (void)dealloc &#123;</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">deinit</span> &#123;</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="5-其他"><a href="#5-其他" class="headerlink" title="5. 其他"></a>5. 其他</h1><h2 id="5-1-内存警告"><a href="#5-1-内存警告" class="headerlink" title="5.1. 内存警告"></a>5.1. 内存警告</h2><p>当<code>app</code>可用内存过低时，系统发出内存警告调用该方法。比如：内存中存放的缓存数据和当前不显示的视图等。</p>
<p>Objec-C代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">- (void)didReceiveMemoryWarning &#123;</span><br><span class="line">  [super didReceiveMemoryWarning];</span><br><span class="line">  &#x2F;&#x2F; TODO:</span><br><span class="line">  if (self.isViewLoaded &amp;&amp; !self.view.window) &#123;</span><br><span class="line">    self.view &#x3D; nil;&#x2F;&#x2F; 目的是让视图重新加载时调用viewDidLoad</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Swift代码示例</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">override</span> <span class="function"><span class="keyword">func</span> <span class="title">didReceiveMemoryWarning</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">super</span>.didReceiveMemoryWarning()</span><br><span class="line">  <span class="comment">// <span class="doctag">TODO:</span></span></span><br><span class="line">  <span class="keyword">if</span> isViewLoaded &amp;&amp; (view.window != <span class="literal">nil</span>) &#123;</span><br><span class="line">    view = <span class="literal">nil</span> <span class="comment">// 目的是让视图重新加载时调用viewDidLoad</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>备考</strong></p>
<p>Debug内存警告：<code>Simulator</code>→<code>Debug</code>→<code>Simulate Memory Warning</code>（快捷键：<code>Shift</code>+<code>command</code>+<code>M</code>）</p>
<h1 id="6-总结"><a href="#6-总结" class="headerlink" title="6. 总结"></a>6. 总结</h1><ul>
<li>上述所有方法中只有<code>initWithNibName</code>方法需要手动调用，其他方法只需要重写即可。</li>
<li>上述所有方法中只有<code>initWithNibName</code>和<code>awakeFromNib</code>方法为处理视图控制器，其他方法都是处理视图。并且这两个方法在视图控制器的生命周期里只会被调用一次。</li>
<li>上述所有方法中<code>viewDidLoad</code>、<code>viewWillAppear</code>和<code>viewDidAppear</code>3个方法经常使用到。在处理视图切换时通常<code>viewDidLoad</code>方法只会调用一次，刷新数据等处理需要在<code>viewWillAppear</code>和<code>viewDidAppear</code>2个方法中处理。</li>
<li><code>didReceiveMemoryWarning</code>方法调用后不会调用<code>dealloc</code>方法，<code>didReceiveMemoryWarning</code>方法中如果不能释放内存，多次被调用后App会直接crash。</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/08/01/hello-world/" rel="prev" title="开 篇">
      <i class="fa fa-chevron-left"></i> 开 篇
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/08/10/%E4%BB%A3%E7%90%86%E5%9B%9E%E8%B0%83%E9%80%9A%E7%9F%A5%E7%9B%91%E5%90%AC/" rel="next" title="iOS - 委托、回调、通知、监听">
      iOS - 委托、回调、通知、监听 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E8%A7%86%E5%9B%BE%E5%88%9B%E5%BB%BA-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">1. 视图创建&#x2F;初始化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-xib%E5%88%9B%E5%BB%BAUIViewController"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. xib创建UIViewController</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-StoryBoard%E4%B8%B2%E8%81%94%E5%9B%BE"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. StoryBoard串联图</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E8%A7%86%E5%9B%BE%E5%8A%A0%E8%BD%BD"><span class="nav-number">2.</span> <span class="nav-text">2. 视图加载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-xib-storyboard-%E6%96%87%E4%BB%B6%E5%8A%A0%E8%BD%BD%E5%AE%8C%E4%BA%86"><span class="nav-number">2.1.</span> <span class="nav-text">2.1. xib(storyboard)文件加载完了</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E5%8A%A0%E8%BD%BD%E8%A7%86%E5%9B%BE"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. 加载视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-%E8%A7%86%E5%9B%BE%E5%8A%A0%E8%BD%BD%E5%AE%8C%E6%88%90"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. 视图加载完成</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-%E8%A7%86%E5%9B%BE%E5%B0%86%E8%A6%81%E5%87%BA%E7%8E%B0"><span class="nav-number">2.4.</span> <span class="nav-text">2.4. 视图将要出现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-%E5%AD%90%E8%A7%86%E5%9B%BE%E5%B0%86%E8%A6%81%E5%B8%83%E5%B1%80"><span class="nav-number">2.5.</span> <span class="nav-text">2.5. 子视图将要布局</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-%E5%AD%90%E8%A7%86%E5%9B%BE%E5%B8%83%E5%B1%80%E5%AE%8C%E6%88%90"><span class="nav-number">2.6.</span> <span class="nav-text">2.6. 子视图布局完成</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-7-%E8%A7%86%E5%9B%BE%E5%B7%B2%E7%BB%8F%E6%98%BE%E7%A4%BA"><span class="nav-number">2.7.</span> <span class="nav-text">2.7. 视图已经显示</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E8%A7%86%E5%9B%BE%E5%8D%B8%E8%BD%BD"><span class="nav-number">3.</span> <span class="nav-text">3. 视图卸载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-%E8%A7%86%E5%9B%BE%E5%B0%86%E8%A6%81%E6%B6%88%E5%A4%B1"><span class="nav-number">3.1.</span> <span class="nav-text">3.1. 视图将要消失</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-%E8%A7%86%E5%9B%BE%E5%B7%B2%E7%BB%8F%E6%B6%88%E5%A4%B1"><span class="nav-number">3.2.</span> <span class="nav-text">3.2.视图已经消失</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E8%A7%86%E5%9B%BE%E9%94%80%E6%AF%81"><span class="nav-number">4.</span> <span class="nav-text">4. 视图销毁</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E5%85%B6%E4%BB%96"><span class="nav-number">5.</span> <span class="nav-text">5. 其他</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-%E5%86%85%E5%AD%98%E8%AD%A6%E5%91%8A"><span class="nav-number">5.1.</span> <span class="nav-text">5.1. 内存警告</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-%E6%80%BB%E7%BB%93"><span class="nav-number">6.</span> <span class="nav-text">6. 总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="xuarmy"
      src="/blog/images/photo.jpg">
  <p class="site-author-name" itemprop="name">xuarmy</p>
  <div class="site-description" itemprop="description">木を植える一番いい時間は十年前、次は今です。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">4</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xuarmy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
